<?php
/*
 * @Description    : ExpansionController
 * @Version        : 1.0.0
 * @Author         : QianLong
 * @Date           : 2020-02-08 15:01:43
 * @LastEditors    : QianLong
 * @LastEditTime   : 2023-11-29 20:19:04
 */

declare(strict_types=1);

namespace App\Controller;

use App\Exception\ApiException;

class ExpansionController extends AbstractController
{
  public function toUrl($id = '', $from = '')
  {
    $where['_id'] = $id;
    $infoData = mongodb2()->web_ads_list->findOne($where);
    $infoData = json_decode(json_encode($infoData), true);
    if (empty($infoData)) {
      throw new ApiException(-1, '内容无效');
    }
    $from = urldecode($from);
    if (empty($from)) {
      $from = $this->request->getHeader('referer');
      if (!empty($from)) {
        $from = $from[0];
      }
    }
    $host = '';
    if (!empty($from)) {
      $parseUrl = parse_url($from);
      if (isset($parseUrl['host'])) {
        $host = $parseUrl['host'];
      }
    }
    $this->insertLog($id, $infoData['uid'], $from, $host);
    if (!empty($from)) {
      $this->insertFromLog($id, $infoData['uid'], $from, $host);
    }
    return $this->response->redirect($infoData['url']);
  }
  public function insertLog($id, $uid, $from, $host)
  {
    $remoteIp = $this->dataResource->getClientIp($this->request);
    $new_uv = $this->redisCache->isNewUv('ads-' . $remoteIp, $id);
    $where['hour'] = date('YmdH');
    $where['a_id'] = $id;
    $hasLog = mongodb2()->web_ads_click_log->findOne($where);
    if (empty($hasLog)) {
      $insert['_id'] = createId();
      $insert['date'] = date('Ymd');
      $insert['hour'] = date('YmdH');
      $insert['only_hour'] = date('H') . ':00';
      $insert['hour_stamp'] = strtotime(date('Y-m-d H') . ':00:00');
      $insert['month'] = date('Ym');
      $insert['year'] = date('Y');
      $insert['create'] = time();
      $insert['from'] = $from;
      $insert['host'] = $host;
      $insert['a_id'] = $id;
      $insert['uid'] = (int) $uid;
      $insert['pv'] = 1;
      $insert['uv'] = 0;
      if ($new_uv == true) {
        $insert['uv'] = 1;
      }
      mongodb2()->web_ads_click_log->insertOne($insert);
    } else {
      if ($new_uv == true) {
        mongodb2()->web_ads_click_log->updateOne($where, ['$inc' => ['pv' => 1, 'uv' => 1]]);
      } else {
        mongodb2()->web_ads_click_log->updateOne($where, ['$inc' => ['pv' => 1]]);
      }
    }
  }
  public function insertFromLog($id, $uid, $from, $host)
  {
    $remoteIp = $this->dataResource->getClientIp($this->request);
    $fromMd5 = md5($from);
    $where['date'] = date('Ymd');
    $where['a_id'] = $id;
    $where['from_md5'] = $fromMd5;
    $hasLog = mongodb2()->web_ads_from_log->findOne($where);
    if (empty($hasLog)) {
      $insert['_id'] = createId();
      $insert['date'] = date('Ymd');
      $insert['hour'] = date('YmdH');
      $insert['hour_stamp'] = strtotime(date('Y-m-d H') . ':00:00');
      $insert['month'] = date('Ym');
      $insert['year'] = date('Y');
      $insert['create'] = time();
      $insert['host'] = $host;
      $insert['from'] = $from;
      $insert['from_ip'] = $remoteIp;
      $insert['from_md5'] = $fromMd5;
      $insert['a_id'] = $id;
      $insert['uid'] = (int) $uid;
      $insert['count'] = 1;
      mongodb2()->web_ads_from_log->insertOne($insert);
    } else {
      mongodb2()->web_ads_from_log->updateOne($where, ['$inc' => ['count' => 1]]);
    }
  }
}
